import { Component,ElementRef,OnInit,Input,ViewChild, Output, EventEmitter,AfterViewInit} from '@angular/core';
import { Http } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { TaskTableComponent } from '../../task/task-table/task-table.component';
declare var $: any;

@Component({
  selector: 'app-task-add',
  templateUrl: './task-add.component.html'
})
export class TaskAddComponent implements OnInit {

  constructor(private server :Service,private http: Http,private tableListService:CudTableService) { }

  ngOnInit() {
    this.ngtaskStatus = true;
  }

  public startTime:any;
  public endTime:any;

  //导入table
  @ViewChild("table")
  table: TaskTableComponent;
  //刷新
  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();
  //清空
  @ViewChild('newlyAddedForm') formElement:HTMLFormElement;
  reset(){
    $("#add").modal("hide");
    
    this.formElement.reset();
    setTimeout(()=> { 
      this.ngtaskStatus = true;
    }, 300)
  }

  public ngtaskStatus:any;

  //新增任务
  submitNewlyAdded(val){
    //console.log(val);
    let url = this.server.baseUrl+'admin/tasks/add';
    // val.startTime = this.server.formatDate(val.startTime);
    // val.endTime = this.server.formatDate(val.endTime);
    let data = val;
    val.taskStatus = Number(val.taskStatus)
    //console.log(data);
    this.http.post(url, data).subscribe((res: any)=>{
      res = res.json();
      this.addOutput.emit(res.result_code);
     // console.log(res)
      if(res.result_code == '0000'){
        $("#add").modal("hide");
        //prompt
        this.server.prompt = {prompt:true,success:true,fail:false,promptStatement:res.message};
        this.server.promptclose;
        this.reset()
      }else{
        this.server.prompt = {prompt:true,success:false,fail:true,promptStatement:res.message};
        this.server.promptclose;
      }
    })
  }
}